/**
 * Created by Limit on 2017/9/7.
 */
function loadJsTree() {
    console.log("jsTree");
    if(typeof ($('#uploadBtn').attr("data-target"))=="undefined"){
        alert("请先选择集群！")
    }else {
        console.log("getCookie(\"clusterId\")"+getCookie("clusterId"));
        $('#jsTreeDemo').data('jstree', false).empty();
        $('#jsTreeDemo').jstree({
            'core': {
                'data': {
                    'url': function (node) {
                        selectNode(node.id);
                        return "/cykmr/hadoop/getHadoopCatalog";
                    },
                    'data': function (node) {
                        return {
                            'id': node.id,
                            "clusterId": getCookie("clusterId")
                        };
                    }
                },
                "check_callback": function (operation, node, parent, name) {
                    node.text = name;
                    operateNode(operation, node);
                    return node.id;
                }
            },
            "plugins": ["contextmenu", "search"]
        });
    }
        $("#file-search").submit(function (e) {
            e.preventDefault();
            $("#jsTreeDemo").jstree(true).search($("#file-search-input").val());
        });
}

function operateNode(operation, node) {
    switch (operation) {
        case "rename_node":
            createNode(node);
            break;

        case "delete_node":
            deleteNode(node);
            break;

    }
}

function createNode(node) {
    var url = "/cykmr/hadoop/createHadoopFolder";
    var parent = node.parent;
    if (parent === "#")
        parent = "/";
    $.ajax({
        async: false,
        url: url,
        dataType: 'json',
        data: {
            id: parent + node.text,
            parent: parent
        },
        success: function (data) {
            node = data;
        }
    });
}

function deleteNode(node) {
    var url = "/cykmr/hadoop/deleteHadoopFolder";
    var parent = node.parent;
    if (parent === "#")
        parent = "/";
    $.ajax({
        async: false,
        url: url,
        dataType: 'json',
        data: {
            id: node.id,
            parent: parent
        },
        success: function (data) {
            console.log(data);
        }
    });
}

function selectNode(path) {
    $("#hdfsInput").val(path);
    //路径确定按钮
     pathEnsure();
}

function pathEnsure() {
    $("#pathBtn").click(function () {
        console.log("hdfsInput.val():"+$("#hdfsInput").val());
     setCookie("path",$("#hdfsInput").val());
    $('#hadoopModal').modal('hide');
});
}